$(function() {
	baseurl=location.protocol+'//'+location.host,//嘟嘟平台环境根路径   
	search=decodeURI(window.location.search.substr(1));
	cust_account=matchQueryString(search,'cust_account');
	app_key=matchQueryString(search,'app_key');
	params=matchQueryString(search,'params');
	showTitle=matchQueryString(search,'showTitle');
	if(showTitle=="")showTitle="0";
	if(params!==''){
		params=getJSONParsedObject(params,'参数格式错误');
	}else
		params=[];
	/***检测账号合法型***/
	$.ajax({
		type: "post",
	      url: baseurl+'/v4/h5/checkAuthenticateAndAccount.do',
	      dataType: "json",
	      async:true,
	      data:{
	    	cust_account:cust_account,
	    	app_key:app_key,
	        sign:matchQueryString(search,'sign'),
	        timestamp:matchQueryString(search,'timestamp')
	      },
	      complete:function(xhr,status){
	        if(loadPrompt){
	          loadPrompt.close();
	          loadPrompt=null;
	        }
	      },
	      beforeSend:function(){
	        loadPrompt=new LoadPrompt('正在校验账户信息，请稍候...');           
	      },
	      success: function(rc){
	    	  if(loadPrompt){
	    		  loadPrompt.close();
	    		  loadPrompt=null;
	    	  }
	    	  if(rc.code!='0000'){
	    		  new UserPrompt(rc.describe);  
	    	  }else{
	    		 
	    		  $('.history h2 span').on('click',function(){
	    			  var l=$(this),loaded=l.attr('loaded'),id=l.attr('id'),context=$(timeSection[id].id);
	    			  //if(!loaded){//第一次加载
	    			  var queryParameter={startTime:moment().subtract(timeSection[id].interval,timeSection[id].unit).format('YYYY-MM-DD 00:00:00')};
	    			  var isAdmin=matchQueryString(search,'isAdmin'); 
	    			  if(isAdmin==='false')
	    				  queryParameter.ext_terminalCode=matchQueryString(search,'userId'); 
	    			    queryMeetingHistoryDetail(queryParameter,{
	    			      content:context,
	    			      label:l
	    			    });
	    			  //}
	    			  l.addClass('current').siblings().removeClass();
	    			  context.addClass('current').siblings().removeClass();
	    			});
	    		  $('.history h2 span').first().trigger('click');
	    	  }
	        
	        
	      }
	});
	/***检测账号合法型***/
	
	var header='<p>date</p><h3><span class="time">times</span><b>type</b><i>cost</i></h3>';
    callee='<dl><dt><span>phone</span></dt><dd>duration</dd></dl>';
	noresult=$('#noresult');
	content=$('#content');
    abilityType={
      TalksV2:'多方通话',
      ScheduleConference:'专业会议',
      Dial:'两方通话',
      Dial_ronglian:'两方通话',
      VoiceNotice3:'语音通知',
      RongLianVoice:'语音通知',
      TianYiVoice:'语音通知',
      AudeoSend:'语音通知',
      RongLianAudeo:'语音通知',
      TianYiAudeo:'语音通知',
      Vcaptcha:'语音验证码',
      Ivr_RongL:'IVR外呼',
      Ivr_Ims:'IVR外呼',
    },timeSection={
      '3d':{
        interval:3,
        unit:'days',
        id:'#3days'
      },
      '1w':{
        interval:1,
        unit:'weeks',
        id:'#1week'
      },
      '1m':{
        interval:1,
        unit:'months',
        id:'#1month'
      },
      '3m':{
        interval:3,
        unit:'months',
        id:'#3months'
      }
    };

	Array.prototype.search=function(value){
	 for (var i = 0; i < this.length; i++) {
	    if(this[i]===value||value.indexOf(this[i])>-1)
	      return {index:i,element:this[i]};
	 };
	 return -1;
	}
	if(showTitle==='0'){
		$('#header').css('display','none');
	    $('#context').css('margin-top',0);
	}
	
	$('#back').on('click',function(){
	 history.back();
	});
	
	
	
	
	/**通话记录***/
	function queryMeetingHistoryDetail(params,elems){
	       $.ajax({
	          type: "get",
	          url:baseurl+'/v4/bill/getBillDetails.do',//查询个人历史通话明细
	          dataType: "jsonp",
	          data:$.extend({
	        	  cust_account:cust_account,
	        	  app_key:app_key,
	        	  ability_type:''
	            },params),
	          jsonp: "callback",
	          success: function(data){
	              if(loadPrompt){
		              loadPrompt.close();
		              loadPrompt=null;
	              }        		 
				if(data.length>0){
					
					var html=[],time,phones,callees,calleeCost;
					for (var i = 0; i < data.length; i++) {
					  time=moment(data[i].createtime,'YYYY-MM-DD HH:mm:ss');
					  
					  if(data[i].ability_type==='Dial' || data[i].ability_type==='Dial_ronglian' ||
							  data[i].ability_type==='TalksV2' || data[i].ability_type==='ScheduleConference'){
						  phones=[data[i].caller].concat(data[i].phones.split(',')); 
					  }else{
						  phones=data[i].phones.split(','); 
					  }
					  
					  callees=data[i].mem.split(',');
					  calleeCost=data[i].times.split(',');
					  html.push(header.replace(/date|times|type|cost/g,function(value){
						 if(value==='date')
						   return time.format('YYYY年MM月DD日');
						 else if(value==='times')
						   return time.format('HH:mm');
						 else if(value==='type')
						   return abilityType[data[i].ability_type]+'（'+phones.length+'人)';//+data[i].call_identifier
						 else if(value==='cost')
						   return data[i].cost+'分钟';
						 else
						   return value;
					   }));
					  //meet_info_callback  
					  for (var m = 0; m < callees.length&&callees[m]; m++) {
						  var target=phones.search(callees[m]);
						  if(target=="-1"){
							  target ={
								index:-1,
								element:callees[m]	  
							  }
							 
						  }
						  var index=target.index;
						  html.push(callee.replace(/phone|duration/g,function(value){
							 if(value==='phone')
							  return target.element;
							 else if(value==='duration'){
								  if(data[i].ability_type==='Dial' || data[i].ability_type==='Dial_ronglian')//两方通话不展示通话时长
									  return '';
								  else{
									  if(calleeCost[m] ===undefined || calleeCost[m] ==='')
										  return "0分钟";
									  else
										  return Math.ceil(calleeCost[m]/60)+'分钟';
								  }
							 }
						  }));
						  if(index!==-1)
							phones.splice(index,1);
					  };
					 //meet_info
					  for (var n = 0; n < phones.length&&phones[n]; n++) {
						   html.push(callee.replace(/phone|duration/g,function(value){
							 if(value==='phone')
							  return phones[n];
							 else if(value==='duration'){
								  if(data[i].ability_type==='Dial' || data[i].ability_type==='Dial_ronglian')//两方通话不展示通话时长
									  return '';
								  else{
									  if(calleeCost[m] ===undefined || calleeCost[m] ==='')
										  return "0分钟";
									  else
										  return Math.ceil(calleeCost[m]/60)+'分钟';
								  }
									 
							 }
						  }));
					   };
					 };
					 elems.content.html(html.join(''));
				}else{
					 noresult.css('display','block');
					 content.css('display','none');
				}
	          },
	          complete:function(){
	             if(loadPrompt){
	               loadPrompt.close();
	               loadPrompt=null;
	             }
	          },
	          beforeSend:function(){
				 elems.content.html('');
	  			 content.css('display','block');
	  			 noresult.css('display','none');
	             loadPrompt=new LoadPrompt('正在查询，请稍后',$(document.body),function(){
	            	var obj=$(document);
	            	this.height(obj.height());
	                this.width(obj.width());
	             })
	          }
	      });
	    }
	
});